from Bases import ChildGroup
from basepage import BasePage
from .. import widgets


class CueStacks(BasePage):
    #button_size = dict(w=.15, h=.075)
    page_name = 'Cue Stacks'
    def __init__(self, **kwargs):
        super(CueStacks, self).__init__(**kwargs)
        #self.orientation = kwargs.get('orientation', 'horizontal')
        self.num_stacks = kwargs.get('num_stacks', 8)
        
        self.button_size = dict(w=1./self.num_stacks, h=.075)
        #w = self.button_size['w']
        #h = self.button_size['h']
        
        #if self.orientation == 'horizontal':
        #    w *= self.num_stacks
        #else:
        #    h *= self.num_stacks
        bounds = [self.x_offset, self.y_offset, 1., self.button_size['h']]
        self.topwidget = self.iOsc.add_widget('Label', name='CueStacks', bounds=bounds)
        #bounds[2] = self.button_size['w']
        #bounds[3] = self.button_size['h']
        self.btn_topwidget = self.topwidget.add_widget(widgets.Label, value='Select Stack', name='select_buttons', bounds=bounds)
        self.SelectBtns = ChildGroup(name='select_buttons',
                                     osc_parent_node=self.osc_node)
                                          
        for x in range(1, self.num_stacks+1):
            #btn = self.SelectBtns.add_child(CueStackSelBtn, Index=x, x_offset=self.x_offset, y_offset=self.y_offset, 
            #                                MainController=self.MainController)
            btn = self.btn_topwidget.add_widget(CueStackSelBtn, Index=x, parent=self)
            self.SelectBtns.add_child(existing_object=btn)
            #self.iOsc.add_widget(btn)
            btn.connect('state_changed', self.on_SelectBtn_state_changed)
            
        #self.multitest = self.topwidget.add_widget(widgets.MultiButton, name='multitest', bounds=[0, .3, .5, .5])
        self.topwidget.refresh_interface()
            
    @property
    def selected_index(self):
        return self._selected_index
    @selected_index.setter
    def selected_index(self, value):
        self._selected_index = value
        for btn in self.SelectBtns.itervalues():
            if btn.Index != value:
                #print btn.Index, btn.touch_state
                btn.touch_state = False
            #else:
            #    btn.selected = True
        if len(self.SelectBtns) == 8:
            print [self.SelectBtns.indexed_items[i].touch_state for i in range(1, 9)]
            
    def on_SelectBtn_state_changed(self, **kwargs):
        btn = kwargs.get('widget')
        state = kwargs.get('state')
        print btn.Index, state
        if state:
            self.selected_index = btn.Index
        #for btn in self.SelectBtns.itervalues():
        #    btn.touch_state = state
            

class CueStackSelBtn(widgets.Button):
    def __init__(self, **kwargs):
        self._selected = False
        self.parent = kwargs.get('parent')
        self.MainController = self.parent.MainController
        self.CueStacks = self.MainController.CueStacks
        i = kwargs.get('Index')
        kwargs.setdefault('name', str(i))
        #kwargs.setdefault('osc_address', str(i))
        
        w = self.parent.button_size['w']
        h = self.parent.button_size['h']
        x = i * w - w
        y = 1. - h
        
        kwargs.setdefault('bounds', [x, y, w, h])
        #kwargs.setdefault('label', str(i))
        #kwargs.update(dict(foregroundColor=[255, 255, 255], backgroundColor=[32, 32, 32, 32], 
        #                   strokeColor=[255, 0, 0]))
        super(CueStackSelBtn, self).__init__(**kwargs)
        if self.Index is None:
            bob
#        lbl_kwargs = kwargs.copy()
#        bounds = lbl_kwargs.get('bounds')[:]
#        bounds[1] = bounds[1] + bounds[3] - .02
#        bounds[3] = .02
#        lbl_kwargs.update({'bounds':bounds, 'value':str(i), 'name':'indexlabel'})
#        lbl = self.add_widget(widgets.Label, **lbl_kwargs)
        
        self.current_stack = None
        stack = self.CueStacks.indexed_items.get(self.Index)
        if stack:
            self.set_current_stack(stack)
        self.CueStacks.bind(child_update=self.on_CueStack_child_update)
        
    def unlink(self):
        self.set_current_stack(None)
        self.CueStacks.unbind(self.on_CueStack_child_update)
        super(CueStackSelBtn, self).unlink()
        
#    @property
#    def selected(self):
#        return self._selected
#    @selected.setter
#    def selected(self, value):
#        if value == self.selected:
#            return
#        self._selected = value
#        self.emit('selected_changed', obj=self, value=value)
        
    def set_current_stack(self, stack):
        if self.current_stack is not None:
            self.current_stack.unbind(self.on_stack_name_changed)
        if stack is None:
            self.label = ''
        else:
            self.label = stack.name
            stack.bind(name=self.on_stack_name_changed)
        self.current_stack = stack
        
    def on_stack_name_changed(self, **kwargs):
        self.label = kwargs.get('value')
        
    def on_CueStack_child_update(self, **kwargs):
        stack = self.CueStacks.indexed_items.get(self.Index)
        if stack != self.current_stack:
            self.set_current_stack(stack)
